package org.ielts.action;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.ielts.domain.base.Campus;
import org.ielts.domain.base.Student;
import org.ielts.profile.IeltsProfile;
import org.ielts.service.CampusService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yifeng.ProfileContext;
import com.yifeng.profile.Profile;
import com.yifeng.project.ProjectConfig;
import com.yifeng.security.authorization.Authorization;
import com.yifeng.sitemap.Catalog;
import com.yifeng.sitemap.Request;
import com.yifeng.sitemap.Scene;
import com.yifeng.sitemap.SiteMap;
import com.yifeng.struts2.action.FormAction;

public class BaseAction extends FormAction {

	@Autowired
	SiteMap siteMap;

	public boolean plainContact(Student stu) {
		if (authorization.hasAuthorized("ielts.student.contact")) {
			return true;
		}
		if (stu.getConsultor() != null) {
			String userId = profile.getUserId();
			if (userId.equals(stu.getConsultor().getId())) {
				return true;
			}
		}
		if (stu.getTutor() != null) {
			String userId = profile.getUserId();
			if (userId.equals(stu.getConsultor().getId())) {
				return true;
			}
		}

		return false;
	}

	public Iterable<Scene> getRootScenes() {
		IeltsProfile profile = (IeltsProfile) Profile.getInstance().getProfile();
		String campus = profile.getCampus();

		Catalog catalog;
		if (StringUtils.isBlank(campus)) {
			catalog = siteMap.findCatalog("全校业务");
		} else {
			catalog = siteMap.findCatalog("校区业务");
		}

		Collection<Scene> scenes = catalog.getChildScenes();

		ArrayList<Scene> returnScenes = new ArrayList<Scene>();

		for (Scene scene : scenes) {
			if (scene.isPresented()) {
				if (authorization.hasAuthorized(scene.getPrivilege())) {
					returnScenes.add(scene);
				}
			}
		}

		catalog = siteMap.findCatalog("公共业务");
		scenes = catalog.getChildScenes();
		for (Scene scene : scenes) {
			if (scene.isPresented()) {
				if (authorization.hasAuthorized(scene.getPrivilege())) {
					returnScenes.add(scene);
				}
			}
		}

		return returnScenes;
	}

	public Profile getProfile() {
		return Profile.getInstance();
	}

	public Iterable<Scene> getSubScenes(Catalog catalog) {
		ArrayList<Scene> scenes = new ArrayList<Scene>();

		readChildCatalog(catalog, scenes);

		return scenes;
	}

	public boolean hasAuthorized(String privilege) {
		return authorization.hasAuthorized(privilege);
	}

	@Autowired
	protected Authorization authorization;

	private void readChildCatalog(Catalog catalog, ArrayList<Scene> returnValue) {

		for (Scene scene : catalog.scenes) {
			String privilege = scene.getPrivilege();
			if (authorization.hasAuthorized(privilege)) {
				returnValue.add(scene);
			}
		}

		for (Catalog subCatalog : catalog.subCatalogs.values()) {
			readChildCatalog(subCatalog, returnValue);
		}
	}

	public Iterable<Catalog> getRootCatalogs() {
		IeltsProfile profile = (IeltsProfile) Profile.getInstance().getProfile();
		String campus = profile.getCampus();

		Catalog catalog;
		if (StringUtils.isBlank(campus)) {
			catalog = siteMap.findCatalog("全校业务");
		} else {
			catalog = siteMap.findCatalog("校区业务");
		}

		Collection<Catalog> catalogs = catalog.getChildCatalog();

		return catalogs;
	}

	public Request getCurrentScene() {
		return SiteMap.findRequest();
	}

	public boolean isCurrentScene(Request request) {
		boolean current = SiteMap.isCurrent(request);

		return current;
	}

	@Autowired
	CampusService campusService;

	public Iterable<Campus> getCampuses() {
		return campusService.findAll();
	}

	public Campus getCurrentCampus() {
		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (StringUtils.isBlank(campusId)) {
			return null;
		}

		Campus campus = campusService.findOne(campusId);

		return campus;
	}

	public ProjectConfig getProjectConfig() {
		ProjectConfig projectConfig = new ProjectConfig();
		projectConfig.setName("扬州环球雅思国际英语");

		return projectConfig;
	}

	public String hiddenNumber(String num) {
		if (StringUtils.isBlank(num)) {
			return "";
		}
		if (num.length() <= 7) {
			String header = num.substring(0, 3);

			StringBuilder sb = new StringBuilder(header);

			for (int i = 3; i < num.length() - 1; i++) {
				sb.append("*");
			}
			sb.append(num.charAt(num.length() - 1));

			return sb.toString();
		} else {
			String header = num.substring(0, 3);
			String end = num.substring(num.length() - 3, num.length());

			StringBuilder sb = new StringBuilder(header);

			for (int i = 3; i < num.length() - 3; i++) {
				sb.append("*");
			}
			sb.append(end);

			return sb.toString();
		}
	}

	public String showMobielNumber(Student student) {
		String mobiel = student.getMobile();
		if (plainContact(student)) {
			return mobiel;
		} else {
			return hiddenNumber(mobiel);
		}
	}

	public String showPhoneNumber(Student student) {
		String phone = student.getPhone();
		if (plainContact(student)) {
			return phone;
		} else {
			return hiddenNumber(phone);
		}
	}

	public String showAddress(Student student) {
		String address = student.getAddress();
		if (plainContact(student)) {
			return address;
		} else {
			return hiddenAddress(address);
		}
	}

	public String hiddenAddress(String address) {
		if (StringUtils.isBlank(address)) {
			return "";
		}

		int num = address.length() / 2;

		String header = address.substring(0, num);

		StringBuilder sb = new StringBuilder(header);

		for (int i = 0; i < num; i++) {
			sb.append("*");
		}

		return sb.toString();
	}
}
